﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using WEBSITE_BANDEAL.App_Code;

namespace WEBSITE_BANDEAL.Admin
{
    public partial class LoaiSP_Insert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lit_title.Text = "Quản lý loại sản phẩm";
            if (!IsPostBack)
            {
                LoadData();
            }


        }

        List<LOAI_SANPHAM> categoryWithParents = new List<LOAI_SANPHAM>();
        protected void load_categories(List<LOAI_SANPHAM> list, int ma, string kytu)
        {
            foreach (LOAI_SANPHAM cat in list)
            {
                if (ma == cat.Maloaicha)
                {
                    cat.Tenloaisp = "|" + kytu + cat.Tenloaisp;
                    categoryWithParents.Add(cat);
                    load_categories(list, cat.Maloai, kytu + kytu); //load that parent
                }
            }
        }
        private void LoadData()
        {
            Loaisanphamcollection list = LOAI_SAN_PHAMBUS.GetAll();
            load_categories(list, 0, "-");
            MaLoaiCha.DataSource = categoryWithParents;
            MaLoaiCha.DataTextField = "Tenloaisp";
            MaLoaiCha.DataValueField = "Maloai";
            MaLoaiCha.DataBind();
            MaLoaiCha.Items.Insert(0, new ListItem("----NO PARENT---", "0"));

        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            LOAI_SANPHAM lsp = new LOAI_SANPHAM();
            lsp.Tenloaisp = Ten.Value;
            lsp.Maloaicha = int.Parse(MaLoaiCha.SelectedValue);
            lsp.Trangthai = 1;
            LOAI_SAN_PHAMBUS.insert(lsp);
            Response.Redirect("LoaiSP_List.aspx");
        }
    }
}